#include <stdio.h>
#include <math.h>
#include "longInt.h"

void
createVec(t_longInt_vector * li, int dim)
{    
    if((li->vec=calloc(dim,sizeof(t_longInt)))==NULL)
        error(1,"Could not allocate memory. Shame on you.");
    
    li->dim = dim;
}

void
turnOn(t_longInt * li, int num)
{
    if(num > CANT_INT * 8 * sizeof(int)-1)
        error(1,"Cannot turn that bit on, out of range.");
    
    (*li)[num/32] |= 1 << num % 32;
}

int
isTrap(t_longInt * l)
{
    return (*l)[CANT_INT]==-1;
}

void
setTrap(t_longInt * l)
{
    (*l)[CANT_INT]=-1;
}

void
clearTrap(t_longInt * l)
{
    (*l)[CANT_INT]=0;
}

void
printState(t_longInt * li)
{
    int i,j;
    printf("[ ");
    for(i=0;i<CANT_INT;i++)
        for(j=0;j<32;j++)
            if(1<<j & (*li)[i])
                printf("%d ",j+32*i);
    printf("]");
}

void
printStates2(t_longInt_vector * l)
{
    int i,j;
    
    for(i=0;i<l->dim;i++)
    {
        printf("Estado[%d]:\t",i);
        printState(l->vec+i);
        printf("\n");
    }
}

void
printStatesDebug(t_longInt_vector * l)
{
    int i,j;
    
    for(i=0;i<l->dim;i++)
    {
        printf("Estado[%d]: ",i);
        for(j=0;j<CANT_INT;j++)
           printf("%u\t",l->vec[i][j]);
        printf("\n");
    }
}

void
reset(t_longInt * l)
{
    int i;
    
    for(i=0;i<CANT_INT;i++)    
        (*l)[i]=0;
}

void
complexOr(t_longInt a, t_longInt b)
{
	int i;
	for(i=0;i<CANT_INT;i++)
             a[i]=a[i] | b[i];
	
}

void
complexAnd(t_longInt a, t_longInt b)
{
	int i;
	for(i=0;i<CANT_INT;i++)
             a[i]=a[i] & b[i];
	
}

int getNextOne(t_longInt a,int from)
{
 int j,indice,i;
 int aux;
  

 for(indice=from/32;indice<CANT_INT;indice++)
 {
     aux=a[indice];          
     j=0;
     while(aux)
     {         if ((j+32*indice)>from && aux %2)
                 return j+32*indice;
               aux/=2;
               j++;
     }        
}
 return -1;
}
